<?php

namespace common\modules\user\models\backend;

use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * UserSearch represents the model behind the search form about `common\modules\user\models\backend\User`.
 */
class UserSearch extends User
{

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status_id', 'join_type', 'group_id', 'created_at', 'updated_at'], 'integer'],
            [['username', 'email', 'password_hash', 'auth_key', 'token', 'role'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/user', 'User List');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = User::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'         => $this->id,
            'status_id'  => $this->status_id,
            'join_type'  => $this->join_type,
            'group_id'   => $this->group_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'password_hash', $this->password_hash])
            ->andFilterWhere(['like', 'auth_key', $this->auth_key])
            ->andFilterWhere(['like', 'token', $this->token])
            ->andFilterWhere(['like', 'role', $this->role]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        $statusArray = self::getStatusArray();
        $roleArray = self::getRoleArray();

        return [
            [
                'attribute' => 'id',
                'filterOptions' => [
                    'style' => 'width:80px',
                ],
            ],

            'username',

            'email',

            [
                'attribute' => 'status_id',
                'format' => 'html',
                'value' => function ($model) {
                    if ($model->status_id === $model::STATUS_ACTIVE)
                    {
                        $class = 'label-success';
                    }
                    elseif ($model->status_id === $model::STATUS_INACTIVE)
                    {
                        $class = 'label-warning';
                    }
                    else
                    {
                        $class = 'label-danger';
                    }

                    return '<span class="label ' . $class . '">' . $model->status . '</span>';
                },
                'width' => '100px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => $statusArray,
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择状态'],
            ],

            [
                'attribute' => 'role',
                'format'    => "html",
                'value'     => function ($model)
                {
                    /** @var UserSearch $model */
                    $roleDescription = $model->rolesDescription();
                    return is_array($roleDescription) ? join('<br />', $roleDescription) : $roleDescription;
                },
                'width' => '200px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => $roleArray,
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择角色'],
            ],

            [
                'attribute' => 'group_id',
                'value' => 'group.name',
                'width' => '200px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => GroupTree::getTreeList(),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择用户组'],
            ],

            [
                'class' => 'common\widgets\grid\ActionColumn',
            ],
        ];
    }
}
